﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace T2ReportsAssistant
{
    public class Settings
    {
        #region Singleton pattern code
        private static Settings _instance;
        private static string _path = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), ".settings");
        private CryptoService _cService;

        private Settings()
        {
            _cService = CryptoService.Instance;
        }

        [XmlIgnore]
        public static Settings Instance
        {
            get
            {
                if (_instance == null)
                {
                    Settings instance = null;
                    try
                    {
                        if (File.Exists(_path))
                        {
                            using (FileStream fs = File.OpenRead(_path))
                                instance = (Settings)Serializer.Deserialize(fs);
                        }
                    }
                    catch
                    {
                    }

                    if (instance == null)
                    {
                        instance = new Settings();
                    }

                    _instance = instance;
                }
                return _instance;
            }
        }

        private static XmlSerializer _serializer;

        private static XmlSerializer Serializer
        {
            get
            {
                XmlAttributes a = new XmlAttributes();
                if (_serializer == null)
                    _serializer = new XmlSerializer(typeof(Settings));
                return _serializer;
            }
        }

        public static void Reload()
        {
            _instance = null;
        }

        public void Save()
        {
            using (FileStream fs = File.Create(_path))
                Serializer.Serialize(fs, _instance);
        }

        public static Settings GetClone()
        {
            return (Settings)Instance.MemberwiseClone();
        }

        public static void AssignSettings(Settings source)
        {
            _instance = source;
        }
        #endregion

        #region IdPassword
        private static string _defaultIdPassword = "password";

        private string _idPassword = _defaultIdPassword;

        [XmlIgnore]
        public string IdPassword
        {
            get { return _idPassword; }
            set
            {
                _idPassword = String.IsNullOrEmpty(value) ? _defaultIdPassword : value;
            }
        }

        [XmlElement(ElementName = "IdPassword")]
        public string IdPasswordCrypted
        {
            get { return _cService.Encrypt(_idPassword); }
            set { _idPassword = _cService.Decrypt(value); }
        }
        #endregion

        #region DestinationFolder
        private static string _defaultDestinationFolder = @"C:\ReportSystem";

        private string _destinationFolder = _defaultDestinationFolder;

        public string DestinationFolder
        {
            get { return _destinationFolder; }
            set
            {
                _destinationFolder = value;
            }
        }
        #endregion

        #region Results file
        private static string _defaultResultsFile = @".queue";

        private string _resultsFile = _defaultResultsFile;

        public string ResultsFile
        {
            get { return _resultsFile; }
            set
            {
                _resultsFile = value;
            }
        }

        private static bool _defaultUseResultsFile = true;

        private bool _useResultsFile = _defaultUseResultsFile;

        public bool UseResultsFile
        {
            get { return _useResultsFile; }
            set
            {
                _useResultsFile = value;
            }
        }
        #endregion

        #region External command
        private static string _defaultExternalCommand = String.Empty;

        private string _externalCommand = _defaultExternalCommand;

        public string ExternalCommand
        {
            get { return _externalCommand; }
            set
            {
                _externalCommand = value;
            }
        }

        private static bool _defaultUseExternalCommand = false;

        private bool _useExternalCommand = _defaultUseExternalCommand;

        public bool UseExternalCommand
        {
            get { return _useExternalCommand; }
            set
            {
                _useExternalCommand = value;
            }
        }

        private static bool _defaultRunAsMinimized = true;

        private bool _runAsMinimized = _defaultRunAsMinimized;

        public bool RunAsMinimized
        {
            get { return _runAsMinimized; }
            set
            {
                _runAsMinimized = value;
            }
        }
        #endregion

        #region SourceView
        private static string _defaultSourceView = "Report System";

        private string _sourceView = _defaultSourceView;

        public string SourceView
        {
            get { return _sourceView; }
            set
            {
                _sourceView = value;
            }
        }
        #endregion

        #region StartDate
        private static DateTime _defaultStartDate = DateTime.MinValue;

        private DateTime _startDate = _defaultStartDate;

        [XmlIgnore]
        public DateTime StartDate
        {
            get { return _startDate; }
            set
            {
                _startDate = value;
            }
        }

        [XmlElement(ElementName = "StartDate")]
        public string StartDateAsString
        {
            get { return _startDate.ToString("HH:mm:ss dd.MM.yyyy"); }
            set
            {
                DateTime.TryParse(value, out _startDate);
            }
        }

        #endregion
    }
}
